/*
 * 3-18-11 Modified to support BenBakerPhoto.com Preview Mode
 		add 'tablename' for for ajax call to publishBtn.click() and fn unsavedChanges()
 				>> serves to abstract the preview mode so all tables and utilize the mode
 * 3-11-11 Modified to support BenBakerPhoto.com Preview Mode
 		Commented out 'deleteAllFiles() in fn deleteConfirmationEvents ()
 		Added methods unsavedChanges() and publishChanges()
 * 10-4-10 Changed sortable call to auto-update sortable on page load
 * 
 */

function deleteBtnEvents () {
	$(".deleteBtn").unbind();
	
/*
	$(".deleteBtn").hover(
		function () {
			//$("#response").html("over");
			$(this).css({background: "#8B0909"});
			$(this).find(".deleteConfirmation").css({background: "#8B0909"});
		}, 
		function () {
			//$("#response").html("out");
			$(this).css({background: "#8B4646"});
			$(this).find(".deleteConfirmation").css({background: "#8B4646"});
		}
	)
*/
	
	$(".deleteBtn").click(
		function() {
			//$("#response").html("clicked");
			$(this).hide();
			$(this).parent().find(".editBtn").hide();
			$(this).css({background: "#8B0909"});
			$(this).unbind();
			$(this).after(deleteConfirmationHTML);
			confirmation = $(this).next();
			confirmation.css({opacity: 0.0, display: "inline", background: "#8B0909"});
			confirmation.animate({opacity: 1}, 300);
			deleteConfirmationEvents();
		}
	);
}

function unsavedChanges() {
	$("#publishChanges").html("You have unsaved changes").fadeIn("slow");
	if (!$("#publishBtn").is(":visible")) {
		$("#publishBtn").fadeIn("slow").effect("highlight", {color:'red'}, 1000);
	}
/* 	$("body").effect("highlight", {color:'#baba43'}, 1000); */
	var tablename = jsTableName.replace('_preview','');
	$.ajax({
		type: "GET",
		url: "ajax.php",
		data: "AJAXtask=markUnpublished&tablename="+tablename,
		success: function(msg){
			//console.log( "Marked unpublished " + msg );
		}
	});	
}

function publishChanges() {
	$("#publishChanges").html("Changes Published!").add("#publishBtn").delay(1500).fadeOut("slow");
}

function deleteConfirmationEvents() {
	$(".deleteYes,.deleteNo").unbind("click");
	$(".deleteYes").click( function() {
		ID = $(this).parent().parent().parent().attr("id");
		buttonsID = $(this).parent().attr("id");
		$(this).parent().html('Deleting...');
		//$.deleteAllFiles(ID);
		$.ajax({
			url: "ajax.php",
			data: "tablename="+jsTableName+"&id="+ID+"&AJAXtask=deleteRecord",
			cache: false,
			complete: function(r){
				$("#"+ID).fadeOut();
				console.log("Finished Deleting : " + r);
				unsavedChanges();
			}
		});
	});
	$(".deleteNo").click(function () {
		$(this).parent().fadeOut("fast", function () {
			$(this).parent().parent().find(".deleteBtn").css({background: "#8B4646"});
		});//css({opacity: 0.0});
		$(this).parent().parent().find(".editBtn").show();
		$(this).parent().parent().find(".deleteBtn").show();
		$(this).parent().remove();
		deleteBtnEvents();
		}
	);	
}

function updateSortOrder(order) {
	var orderString = '';
	//var order = $('.sortable').sortable('toArray');
	for (i=0;i<order.length;i++) {
		if (i != 0) { 
			orderString = orderString + '&'; 
		}
		orderString = orderString + 'order[]='+order[i];
	}
	//alert(order);
	//alert(orderString);
	//test[]=2&test[]=1&test[]=3 	
	$.ajax({
		type: "GET",
		url: "ajax.php",
		data: "AJAXtask=sortorderUpdate&tablename="+jsTableName+"&"+orderString,
		success: function(msg){
			//console.log( "Data Saved: " + msg );
		}
	});	
}

var deleteConfirmationHTML = "<div class='deleteConfirmation'>Do you really want to delete? <a class='deleteYes'>YES</a> // <a class='deleteNo'>NO</a></div";

$(function() {
	deleteBtnEvents();
	//$("img:first").before("<div id='response'>response</div>");
	$("#listViewBtn").click( function() {
	$(".backListRecord").css({ "float":"none", "width":"600px" });
	$(this).css({"background":"#8F8E79 none repeat scroll 0 0"});
	$("#gridViewBtn").css({"background":"none"});
	});
	$("#gridViewBtn").click( function() {
	$(".backListRecord").css({ "float":"left", "width":"300px" });
	$(this).css({"background":"#8F8E79 none repeat scroll 0 0"});
	$("#listViewBtn").css({"background":"none"});
	});
	
	$(".sortable").sortable({ 
		items: '.backListRecord',
		handle: '.dragIcon',
		update: function() {
			var order = $('.sortable').sortable('toArray');
			updateSortOrder(order);
			unsavedChanges();
		}
	});
	// execute an update to sort_order >> if new record is created
	// but not manually sorted, order didn't use to update 10-4-10
	if( $('.sortable').length > 0 )	{
		var order = $('.sortable').sortable('toArray');
		updateSortOrder(order);
	}

	$("#publishBtn").click(function () {
		var tablename = jsTableName.replace('_preview','');
		$.ajax({
			type: "GET",
			url: "ajax.php",
			data: "AJAXtask=publishChanges&tablename="+tablename,
			success: function(r){
				console.log( "Changes Saved: " + r );
				publishChanges();
			}
		});	
	
	});
	
	$(".dragIcon").hover(
		function() {$(this).css("background-position","0px -15px");},
		function() {$(this).css("background-position","0px 0px");}
	);
/*
	$(".currentTable").toggle(
		function () {
			$(".otherTable").fadeIn("fast");
			$(".currentPage").css("display","none");
		},
		function () {
			$(".otherTable").fadeOut("fast", function () {
				$(".currentPage").css("display","block");      
			});
		}
	);
*/
	
/*
	$(".newBtn,.editBtn").hover(
		function () {
			$(this).css({background: "#5F5E50"});
		}, 
		function () {
			$(this).css({background: "#8F8E79"});
		}
	);
*/
});


